package com.woniuxy.gateway.config;


import com.woniuxy.core.exception.authenticationException.AuthenticationException;
import com.woniuxy.core.exception.authenticationException.AuthenticationExceptionCode;
import com.woniuxy.core.util.JwtUtil;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.util.AntPathMatcher;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

import javax.annotation.Resource;
import java.util.List;


@Order(1)
@Component
public class AuthenticationFilter implements GlobalFilter {


    @Resource
    private UrlParseConfig urlParseConfig;

    AntPathMatcher pm = new AntPathMatcher();

//    private Mono<Void> write(ServerWebExchange exchange){
//        Result<?> result = Result.getResult().setCode(401).setMessage("未登录");
//        String json = JSONUtil.parse(result).toString();
//        exchange.getResponse().getHeaders().setContentType(MediaType.APPLICATION_JSON);
//        return exchange.getResponse().writeWith(Mono.fromSupplier(() -> {
//            return exchange.getResponse().bufferFactory().wrap(json.getBytes());
//        }));
//    }

    public boolean isContain(String path,List<String> urls){
        if(urls!=null){
            for (String url : urls) {
                if (pm.match(url,path)) return true;
            }
        }
        return false;
    }


    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        System.out.println("进入全局拦截器");
        String url = exchange.getRequest().getPath().toString();
        System.out.println(url);
        //是否是公共放行接口
        if(isContain(url,urlParseConfig.getBaseUrl())){
            return chain.filter(exchange);
        }

        //获取token
        String token = null;
        List<String> ts = exchange.getRequest().getHeaders().get("token");
        if(ts!=null&&ts.size()>0){
            token = ts.get(0);
        }

        //判定当前访问的接口是用户接口还是管理系统
        if(url.endsWith(".api")){
            //是否放行
            if (isContain(url,urlParseConfig.getAdminUrl())){
                return chain.filter(exchange);
            }else {
                //管理系统解析
                try {
                    JwtUtil.parseAdminToken(token);
                } catch (Exception e) {
                    e.printStackTrace();
                    throw new AuthenticationException(AuthenticationExceptionCode.TOKEN_INVALID);
                }
            }
        }else {
            //是否放行
            if (isContain(url,urlParseConfig.getUserUrl())){
                return chain.filter(exchange);
            }else {
                //用户系统解析
                try {
                    JwtUtil.parseUserToken(token);
                } catch (Exception e) {
                    e.printStackTrace();
                    throw new AuthenticationException(AuthenticationExceptionCode.TOKEN_INVALID);
                }
            }
        }
        //通过
        return chain.filter(exchange);
    }
}
